package demo.hms.Reserve.service.servicelmpl;

import demo.hms.Reserve.dao.ReserveDao;
import demo.hms.Reserve.service.ReserveService;
import demo.hms.ReserveRecords.service.ReserveRecordsService;
import demo.hms.mybatis.entity.Live;
import demo.hms.mybatis.entity.Reserve;
import demo.hms.mybatis.entity.ReserveRecords;
import demo.hms.stay.service.StayService;
import demo.hms.util.Result;
import demo.hms.util.ResultSubmit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED,rollbackFor = Exception.class)
public class ReserveServicelmpl implements ReserveService {
    @Resource
    private ReserveDao dao;
    @Resource
    private ReserveRecordsService reserveRecordsService;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Resource
    private StayService stayService;
    @Override
    public Result selectReserve(Reserve reserve) {
        return ResultSubmit.success(dao.queryAll(reserve));
    }
    @Override
    public Result insertReserve(Reserve reserve) {
        reserve.setDeleteIdentity("未删除");reserve.setReserveTime(format.format(new Date()));
        reserve.setReserveId(Math.abs((int) System.currentTimeMillis()));
        reserve.setAbrogateIdentity("未作废");reserve.setLiveIdentity("未收款");
        reserve.setDataUpdateTime(format.format(new Date()));
        ReserveRecords reserveRecords = new ReserveRecords();
        reserveRecords.setReserveId(reserve.getReserveId());
        reserveRecords.setReserveRecordsId(Math.abs((int) System.currentTimeMillis()));
        reserveRecords.setCustomerId(reserve.getCustomerId());reserveRecords.setRoomId(reserve.getRoomId());
        reserveRecords.setDataUpdateTime(format.format(new Date()));reserveRecords.setDeleteIdentity("未删除");
        if (dao.insert(reserve) == 1 && reserveRecordsService.insert(reserveRecords) == 1) {return ResultSubmit.success();} else {
            // 手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();return ResultSubmit.fail();
        }
    }
    @Override
    public Result reserveUpdateLive(Reserve reserve) {
        reserve = dao.queryAll(reserve).get(0);
        Live live = new Live();
        live.setCustomerId(reserve.getCustomerId());
        live.setRoomId(reserve.getRoomId());
        Result result = stayService.insertStay(live);
        reserve.setLiveId(Integer.valueOf(result.getData().toString()));
        int i = dao.update(reserve);
        int j = dao.insert(reserve);
        if (result.getCode() == 200 && i ==1 && j == 1 ){
            return ResultSubmit.success();} else {
            // 手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.fail();
        }
    }
    @Override
    public Result cancelReserve(Reserve reserve) {
        reserve = dao.queryAll(reserve).get(0);
        int i = dao.update(reserve);
        reserve.setDeleteIdentity("已删除");
        int j = dao.insert(reserve);
        if(i == 1 && j == 1){
            return ResultSubmit.success();
        }else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.fail();
        }
    }
    @Override
    public Result abrogateReserve(Reserve reserve) {
        reserve = dao.queryAll(reserve).get(0);
        int i = dao.update(reserve);
        reserve.setAbrogateIdentity("已作废");
        int j = dao.insert(reserve);
        if(i == 1 && j == 1){
            return ResultSubmit.success();
        }else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.fail();
        }
    }

}
